package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.EncyclopediasAdapter;
import com.hongxun.design.bean.EncyclopediaBean;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/20.
 * 百科收藏
 */
public class CollectActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    TextView back, search;
    LinearLayout title;
    EditText input;
    SwipeRefreshListView listview;
    private Handler listHandler;
    String name = "";
    int startNum = 0;
    int addNum =  MyApp.addNum;
    List<EncyclopediaBean> collectData;
    EncyclopediasAdapter mAdapter;
    boolean flag = false;
    String tag = "CollectActivity_";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        findView();
    }

    @Override
    protected void onResume() {
        MyApp.activity = this;
        mActivity = this;
        baseActivity = this;
        if (flag) {
            MyApp.HTTP.http.isLoad = false;
            onRefresh();
        } else {
            flag = true;
        }

        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();
        try {
            name = getIntent().getExtras().getString(MString.TITLE,"");
        }catch (Exception e){}

        back = (TextView) findViewById(R.id.collect_back);
        search = (TextView) findViewById(R.id.collect_search);
        title = (LinearLayout) findViewById(R.id.collect_title);
        input = (EditText) findViewById(R.id.collect_input);
        input.setText(name);

        SetTypeface.set(search, IconSourse.SEARCH);//搜索
        SetTypeface.set(back, IconSourse.BACK);
        search.append(" 搜索");
        listview = (SwipeRefreshListView) findViewById(R.id.collect_list);
        listview.setOnRefreshListener(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRefresh();
            }
        });
        addListHandler();
        try {
            collectData = MyApp.File.LoadFileData_CollectList(tag + name);
            if (collectData != null && collectData.size() > 0) {
                listHandler.sendEmptyMessage(1);
            }
        }catch (Exception e){}


        onRefresh();
    }

    private void getList() {
        MyApp.HTTP.getCollectList(listHandler, HttpStringUtil.SUCCESSED, input.getText().toString(), startNum, startNum + addNum);
    }

    private void addListHandler() {
        listHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (collectData == null) {
                                collectData = FastJsonUtil.JsonToGetCollectList((String) msg.obj);
                            } else {
                                collectData.clear();
                                collectData.addAll(FastJsonUtil.JsonToGetCollectList((String) msg.obj));
                            }
                            listHandler.sendEmptyMessage(1);
                            MyApp.File.SaveFileData(tag + name, "{\"collectList\":" + JSON.toJSONString(collectData) + "}");
                            startNum += addNum;
                        } else {
                            listview.setRefreshing(false);
                            ToastShow.showShort(mActivity, FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        if (mAdapter == null) {
                            mAdapter = new EncyclopediasAdapter(mActivity, collectData);
                            listview.setAdapter(mAdapter);
                            listview.setFragment(baseActivity);
                            listview.setCanScroll(true);
                            listview.setDivider(1);
                        } else {
                            listview.update();
                            listview.setRefreshing(false);
                            listview.removeFooterView();
                        }

                        break;
                    case HttpStringUtil.ADDMROE:
                        String s2 = FastJsonUtil.JsonToCheck_String((String) msg.obj);
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            collectData.addAll(FastJsonUtil.JsonToGetCollectList((String) msg.obj));
                            startNum += addNum;
                            listHandler.sendEmptyMessage(1);
                        } else {
                            ToastShow.showShort(mActivity, s2);
                        }

                        break;

                    case HttpStringUtil.ERROR:
                        listview.setRefreshing(false);
                        break;
                }
            }
        };
    }

    @Override
    public void onRefresh() {
        startNum = 0;
        getList();
    }

    @Override
    public void addMore() {
        super.addMore();
        MyApp.HTTP.getCollectList(listHandler, HttpStringUtil.ADDMROE, name, startNum, startNum + addNum);
    }
}
